﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Customer_Profile.eCard.DBDao;
using KVB.Common;
using Customer_Profile.Admin;
using System.IO;


namespace Customer_Profile.eCard
{
    public partial class eCard_input : System.Web.UI.Page
    {
        const string UploadDirectory = "~/eCard/tempUpload/";
        const int ThumbnailSize = 100;

        public string strScript = "";
        string strUserName = "";
        string strUserID = "";
        string strEntityID = "";
        string strEntity = "";
        string strDealNO = "";
        protected void Page_Load(object sender, EventArgs e)
        {   
            if(Request["dealno"]!=null)
            {
                strDealNO = Request["dealno"].ToString();
            }
         
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {                
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntityID = ((UserInfoEntity)Session["usermodel"]).UserEntityID;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }
            
            string strURL = HttpContext.Current.Request.Url.AbsolutePath;
            bool bok = Comm.ifhasright(strUserID, strURL);
            if (!bok)
            {
                Response.Write("你无权访问此页面");
                Response.End();
            }

            if (hiddealno.Value.ToString() == "")
            {
                btnCancel.Enabled = false;
                btnSubmit.Enabled = false;
            }
            else
            {
                btnCancel.Enabled = true;
                btnSubmit.Enabled = true;
            }

            if (!Page.IsPostBack)
            {
                txtRealAmount.Attributes.Add("onkeydown", "if(event.keyCode==13)event.keyCode=9"); 
                txtHQ.Attributes.Add("onkeydown", "if(event.keyCode==13)event.keyCode=9"); 
                txtRealdiscount.Attributes.Add("onkeydown", "if(event.keyCode==13)event.keyCode=9"); 

                txtAgentRealAmount.Attributes.Add("onkeydown", "if(event.keyCode==13)event.keyCode=9");
                txtAgentHQ.Attributes.Add("onkeydown", "if(event.keyCode==13)event.keyCode=9");
                txtRealSaleDiscount.Attributes.Add("onkeydown", "if(event.keyCode==13)event.keyCode=9"); 

                Comm.BindDll(ref dropIDtype, "IDType", false, "");

                DataSet dsEntity = EntityDao.getEntityList("N");
                dropSaleEntity.DataSource = dsEntity;
                dropSaleEntity.DataTextField = "entity_name";
                dropSaleEntity.DataValueField = "entity_id";
                dropSaleEntity.DataBind();
                dropSaleEntity.ClearSelection();
                dropSaleEntity.Items.FindByValue(strEntityID).Selected = true;

                DataSet dsProd = ProductDao.getAllProduct("Y");
                dropProd.DataSource = dsProd;
                dropProd.DataTextField = "procduct_name";
                dropProd.DataValueField = "procduct_code";
                dropProd.DataBind();
                //dropDueProd
                dropDueProd.DataSource = dsProd;
                dropDueProd.DataTextField = "procduct_name";
                dropDueProd.DataValueField = "procduct_code";
                dropDueProd.DataBind();
                ListItem liAll = new ListItem();
                liAll.Text = "取款";
                liAll.Value = "";
                dropDueProd.Items.Insert(0, liAll);

                DataView dvTransferType = Comm.BindDevType("payMethod");
                transfer_type.DataSource = dvTransferType;
                transfer_type.TextField = "CodeName";
                transfer_type.ValueField = "CodeValue";
                transfer_type.DataBind();

                DataView dvtoAccount = Comm.BindDevType("netbank_name");
                to_account.DataSource = dvtoAccount;
                to_account.TextField = "CodeName";
                to_account.ValueField = "CodeValue";
                to_account.DataBind();

                if (strDealNO.Equals(""))
                {
                    string[] strDiscount = dropProd.SelectedItem.Text.Split('|');
                    txtRealdiscount.Value = strDiscount[2].ToString().Trim();
                    checkAgentSale.Checked = false;
                    tbAgentSale.Visible = false;
                    tbNormal.Visible = true;
                    txtDealer.Value = strUserName;
                }
                else
                {
                    FillData(strDealNO);
                    btnCancel.Enabled = true;
                    btnSubmit.Enabled = true;
                    txtCardno.Disabled = true;
                    checkAgentSale.Enabled = false;
                }
            }
        }

        private void FillData(string DealNo)
        {
            try
            {
                hiddealno.Value = DealNo;
                DataSet dsDeals = DealDao.EntitySearchDealbyDealno(DealNo);
                #region 填充客户信息
                txtcpartyno.Text = dsDeals.Tables[1].Rows[0]["cparty_id"].ToString();
                txtName.Value = dsDeals.Tables[1].Rows[0]["cparty_name"].ToString();                
                dropIDtype.ClearSelection();
                dropIDtype.Items.FindByValue(dsDeals.Tables[1].Rows[0]["id_type"].ToString()).Selected = true;

                txtIDcode.Value = dsDeals.Tables[1].Rows[0]["id_no"].ToString();
                txtPhone.Value = dsDeals.Tables[1].Rows[0]["phone"].ToString();
                txtAddress.Value = dsDeals.Tables[1].Rows[0]["address"].ToString();
                #endregion

                if (dsDeals.Tables[0].Rows[0]["if_agentsale"].ToString().Equals("Y"))
                {
                    tbNormal.Visible = false;
                    tbAgentSale.Visible = true;
                    agentLoad();
                    dropAgentName.Items.FindByValue(dsDeals.Tables[0].Rows[0]["agent_id"].ToString()).Selected = true;
                    dropProdAgent.Items.FindByValue(dsDeals.Tables[0].Rows[0]["prod_code"].ToString()).Selected = true;
                    dropDueProdAgent.Items.FindByValue(dsDeals.Tables[0].Rows[0]["due_prodcode"].ToString()).Selected = true;
                    txtDueProdRemarkAgent.Value = dsDeals.Tables[0].Rows[0]["due_prodremark"].ToString();
                    txtCardnoAgent.Value = dsDeals.Tables[0].Rows[0]["card_no"].ToString();
                    txtAgentRealAmount.Value = dsDeals.Tables[0].Rows[0]["initial_amount"].ToString();
                    txtAgentHQ.Value = dsDeals.Tables[0].Rows[0]["should_amount"].ToString();
                    txtRealSaleDiscount.Value = dsDeals.Tables[0].Rows[0]["real_discount"].ToString();
                    txtAgentIncardAmount.Value = dsDeals.Tables[0].Rows[0]["due_amount"].ToString();
                    txtAgentSpread.Value = dsDeals.Tables[0].Rows[0]["amount_spread"].ToString();
                    checkAgentSale.Checked = true;
                    tbAgentSale.Visible = true;
                    tbNormal.Visible = false;
                }
                else
                {
                    dropProd.Items.FindByValue(dsDeals.Tables[0].Rows[0]["prod_code"].ToString()).Selected = true;
                    dropDueProd.Items.FindByValue(dsDeals.Tables[0].Rows[0]["due_prodcode"].ToString()).Selected = true;
                    txtDueProdRemark.Value = dsDeals.Tables[0].Rows[0]["due_prodremark"].ToString();
                    txtCardno.Value = dsDeals.Tables[0].Rows[0]["card_no"].ToString();
                    txtRealAmount.Value = dsDeals.Tables[0].Rows[0]["initial_amount"].ToString();
                    txtHQ.Value = dsDeals.Tables[0].Rows[0]["should_amount"].ToString();
                    txtRealdiscount.Value = dsDeals.Tables[0].Rows[0]["real_discount"].ToString();
                    txtIncardAmount.Value = dsDeals.Tables[0].Rows[0]["due_amount"].ToString();
                    txtSpread.Value = dsDeals.Tables[0].Rows[0]["amount_spread"].ToString();
                    checkAgentSale.Checked = false;
                    tbAgentSale.Visible = false;
                    tbNormal.Visible = true;                    
                }

                dropSaleEntity.Items.FindByValue(dsDeals.Tables[0].Rows[0]["entity_Id"].ToString()).Selected = true;                
                txtDesignee.Text = dsDeals.Tables[0].Rows[0]["Designee"].ToString();
                txtDealer.Value = dsDeals.Tables[0].Rows[0]["dealer"].ToString();
                txtRemark.Value = dsDeals.Tables[0].Rows[0]["remark"].ToString();
                txtBackReason.Value = dsDeals.Tables[0].Rows[0]["back_reason"].ToString();
                bindBankTransfer();
                bindAttachList(int.Parse(DealNo));
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnDealSave_Click(object sender, EventArgs e)
        {
            try
            {
                string flag = "";
                string prod_code = "";
                string due_prodcode = "";     //
                string due_prodremark = "";   //
                string if_especial = "";      //
                string especial_remark = "";  //
                string Designee = "";
                string period = "";
                string card_no = "";
                decimal real_amount;
                decimal should_amount;
                decimal stand_discount;
                decimal real_discount;
                decimal incard_amount;
                decimal amount_spread;
                string value_date = "";
                string due_date = "";
                string agentname = "";
                string entity_id = "";
                string entity = "";
                string dealer = "";
                string remark = "";
                string cparty_id = txtcpartyno.Text;
                string strDealno = "";
                string if_agentsale = "";

                if (checkAgentSale.Checked)
                {
                    if_agentsale = "Y";
                }
                else
                {
                    if_agentsale = "N";
                }

                if (hiddealno.Value != "")
                {
                    flag = "UPDATE";
                    strDealno = hiddealno.Value;
                }
                else
                {
                    flag = "INSERT";
                }

                if (checkAgentSale.Checked)
                {
                    agentname = dropAgentName.SelectedItem.Value;
                    prod_code = dropProdAgent.SelectedItem.Value;
                    due_prodcode = dropDueProdAgent.SelectedItem.Value;
                    due_prodremark = txtDueProdRemarkAgent.Value;
                    period = dropProdAgent.SelectedItem.Text.Split('|')[1].ToString();//dropTerm.SelectedItem.Value;
                    card_no = txtCardnoAgent.Value.Trim();
                    real_amount = decimal.Parse(txtAgentRealAmount.Value);
                    should_amount = decimal.Parse(txtAgentHQ.Value);
                    real_discount = decimal.Parse(txtRealSaleDiscount.Value);
                    stand_discount = decimal.Parse(dropProdAgent.SelectedItem.Text.Split('|')[2].ToString().Trim());
                    incard_amount = decimal.Parse(txtAgentIncardAmount.Value);
                    amount_spread = decimal.Parse(txtAgentSpread.Value);
                }
                else
                {
                    prod_code = dropProd.SelectedItem.Value;
                    due_prodcode = dropDueProd.SelectedItem.Value;
                    due_prodremark = txtDueProdRemark.Value;
                    period = dropProd.SelectedItem.Text.Split('|')[1].ToString();//dropTerm.SelectedItem.Value;
                    card_no = txtCardno.Value.Trim();
                    real_amount = decimal.Parse(txtRealAmount.Value);
                    should_amount = decimal.Parse(txtHQ.Value);
                    real_discount = decimal.Parse(txtRealdiscount.Value);
                    stand_discount = decimal.Parse(dropProd.SelectedItem.Text.Split('|')[2].ToString().Trim());
                    incard_amount = decimal.Parse(txtIncardAmount.Value);
                    amount_spread = decimal.Parse(txtSpread.Value);
                }

                if_especial = "N"; //dropEspecial.SelectedItem.Value;
                especial_remark = "";//txtEspecialRemark.Value;
                Designee = txtDesignee.Text;
                entity_id = dropSaleEntity.SelectedItem.Value;
                entity = dropSaleEntity.SelectedItem.Text;
                dealer = txtDealer.Value;
                remark = txtRemark.Value.Trim();

                if (cparty_id.Equals(""))
                {
                    lbWarning.Text += "请先选择客户!";
                }
                else
                {
                    if (flag.Equals("INSERT"))
                    {
                        //1,检查卡号是否可用。如果是添加成功，卡号要enable ，防止换卡号，
                        string strStatsu = "";
                        string strMessage = "";
                        DealDao.checkCardSatus(card_no, out strStatsu, out strMessage);
                        if (strStatsu.Equals("N"))
                        {
                            lbWarning.Text = strMessage;
                            return;
                        }
                        strDealno = DealDao.AddMMDeal(if_agentsale, agentname, cparty_id, prod_code, due_prodcode, due_prodremark, card_no, real_amount, should_amount, stand_discount, real_discount, incard_amount, amount_spread, value_date, due_date, entity_id,entity, dealer, strUserID, if_especial, especial_remark, remark,Designee);
                        hiddealno.Value = strDealno;
                        if (!strDealno.Equals(""))
                        {
                            btnCancel.Enabled = true;
                            btnSubmit.Enabled = true;
                            txtCardno.Disabled = true;
                            checkAgentSale.Enabled = false;
                            lbWarning.Text = "提示信息：添加成功！";
                        }
                        else
                        {
                            lbWarning.Text = "提示信息：添加失败！";
                        }
                    }
                    else
                    {
                        bool bok = DealDao.UpdateMMDeal(strDealno, if_agentsale, agentname, cparty_id, prod_code, due_prodcode, due_prodremark, card_no, real_amount, should_amount, stand_discount, real_discount, incard_amount, amount_spread, value_date, due_date, entity_id,entity, dealer, strUserID, if_especial, especial_remark, remark, Designee);
                        if (bok)
                        {
                            btnCancel.Enabled = true;
                            btnSubmit.Enabled = true;
                            lbWarning.Text = "提示信息：修改成功！";
                        }
                        else
                        {
                            lbWarning.Text = "提示信息：修改失败！";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void serach_Click(object sender, EventArgs e)
        {
            try
            {
                string strIDno = txtsearchno.Value.Trim();
                bindCpartInfo(strIDno);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        private void bindCpartInfo(string strIDno)
        {
            try
            {
                DataSet ds = CpartyDao.SearchCpartybyIDno(strIDno);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    txtcpartyno.Text = ds.Tables[0].Rows[0]["cparty_id"].ToString();
                    txtName.Value = ds.Tables[0].Rows[0]["cparty_name"].ToString();
                    dropSaleEntity.Items.FindByValue(strEntityID).Selected = true;
                    dropIDtype.ClearSelection();
                    dropIDtype.Items.FindByValue(ds.Tables[0].Rows[0]["id_type"].ToString()).Selected = true;

                    txtIDcode.Value = ds.Tables[0].Rows[0]["id_no"].ToString();
                    txtPhone.Value = ds.Tables[0].Rows[0]["phone"].ToString();
                    txtAddress.Value = ds.Tables[0].Rows[0]["address"].ToString();

                    txtDesignee.Text = ds.Tables[0].Rows[0]["cparty_name"].ToString() + "/" + ds.Tables[0].Rows[0]["id_no"].ToString() + "/" + ds.Tables[0].Rows[0]["phone"].ToString();
                }
                else
                {
                    txtsearchno.Value = "没有找到相关信息！";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("mmdealSearch.aspx", true);
        }

        private void bindBankTransfer()
        {
            try
            {
                DataSet dsBankTransfer = TransferDao.getTransfers(hiddealno.Value);
                gridBank.DataSource = dsBankTransfer;
                gridBank.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        private bool checkTransferForm()
        {
            if (account_name.Text.Trim().Equals(""))
            {
                lblTransferWarning.Text = "账户名称不能为空";
                KVB.Common.JavaScript.ViewAlertMsg("账户名称不能为空");
                return false;
            }
            if (account_no.Text.Trim().Equals(""))
            {
                lblTransferWarning.Text = "账户号不能为空";
                KVB.Common.JavaScript.ViewAlertMsg("账户号不能为空");
                return false;
            }
            if (bank_name.Text.Trim().Equals(""))
            {
                lblTransferWarning.Text = "银行名称不能为空";
                KVB.Common.JavaScript.ViewAlertMsg("银行名称不能为空");
                return false;
            }
            if (transfer_amount.Text.Trim().Equals(""))
            {
                lblTransferWarning.Text = "汇款金额不能为空";
                KVB.Common.JavaScript.ViewAlertMsg("汇款金额不能为空");
                return false;
            }
            if (transfer_date.Text.Trim().Equals(""))
            {
                lblTransferWarning.Text = "汇款日期不能为空";
                KVB.Common.JavaScript.ViewAlertMsg("汇款日期不能为空");
                return false;
            }
            if (transfer_type.SelectedIndex<0)
            {
                lblTransferWarning.Text = "付款方式不能为空";
                KVB.Common.JavaScript.ViewAlertMsg("付款方式不能为空");
                return false;
            }
            if (to_account.Text.Trim().Equals(""))
            {
                lblTransferWarning.Text = "转入账户不能为空";
                KVB.Common.JavaScript.ViewAlertMsg("转入账户不能为空");
                return false;
            }
            return true;
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (hiddealno.Value.ToString().Trim().Equals(""))
                {
                    lblTransferWarning.Text = "提示信息：请先完成交易信息数据的保存后再添加汇款信息！";
                    return;
                }
                string ideal_no = hiddealno.Value;

                string icard_no = "";
                if (checkAgentSale.Checked)
                {
                    icard_no = txtCardnoAgent.Value;
                }
                else
                {
                    icard_no = txtCardno.Value;
                }

                if (!checkTransferForm())
                {
                    return;
                }

                string iaccount_name = account_name.Text;
                string ibank_name = bank_name.Text;
                string iaccount_no = account_no.Text;
                string itransfer_type = transfer_type.SelectedItem.Text;
                string itransfer_amount = transfer_amount.Text;
                decimal dtransfer_amount;
                try
                {
                   dtransfer_amount= Convert.ToDecimal(itransfer_amount);
                }
                catch
                {
                    lblTransferWarning.Text = "提示信息：添加cflow数据失败！请正确填写金额";
                    return;
                }

                string ito_account = to_account.Text;
                string itransfer_date = transfer_date.Text;
                string iremark = remark.Text;

                bool bok = TransferDao.AddTransfer(ideal_no, icard_no, iaccount_name, ibank_name, iaccount_no, itransfer_type, dtransfer_amount, ito_account, itransfer_date, iremark, strUserID);
                if (!bok)
                {
                    lblTransferWarning.Text = "提示信息：添加cflow数据失败，请联系系统管理员！";
                }
                else
                {
                    lblTransferWarning.Text = "提示信息：添加cflow数据成功！";
                }
                bindBankTransfer();
                //gridBank.Selection.UnselectAll();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                string itransfer_id = transfer_id.Value;
                if (itransfer_id == null || itransfer_id.Trim().Equals(""))
                {
                    lblTransferWarning.Text = "提示信息：" + "修改cflow数据请先选择要修改的数据！";
                    return;
                }
                if (!checkTransferForm())
                {
                    return;
                }
                string ideal_no = hiddealno.Value.ToString();
                //string icard_no = txtCardno.Value;
                string icard_no = "";
                if (checkAgentSale.Checked)
                {
                    icard_no = txtCardnoAgent.Value;
                }
                else
                {
                    icard_no = txtCardno.Value;
                }
                string iaccount_name = account_name.Text;
                string ibank_name = bank_name.Text;
                string iaccount_no = account_no.Text;
                string itransfer_type = transfer_type.SelectedItem.Text;
                string itransfer_amount = transfer_amount.Text;
                string ito_account = to_account.Text;
                string itransfer_date = transfer_date.Text;
                string iremark = remark.Text;

                if (itransfer_id != "")
                {
                    bool bok = TransferDao.UpdateTransfer(itransfer_id, icard_no, iaccount_name, ibank_name, iaccount_no, itransfer_type, itransfer_amount, ito_account, itransfer_date, iremark);
                    if (!bok)
                    {
                        lblTransferWarning.Text = "提示信息：" + "修改cflow数据失败，请联系系统管理员！";
                    }
                    else
                    {
                        lblTransferWarning.Text = "提示信息：" + "修改cflow数据成功！";
                        transfer_id.Value = itransfer_id;
                    }
                }
                bindBankTransfer();
                //gridBank.Selection.UnselectAll();
            }
            catch(Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string itransfer_id = transfer_id.Value;
                if (itransfer_id != "")
                {
                    bool bok = TransferDao.deleteTransfer(itransfer_id);
                    if (!bok)
                    {
                        lblTransferWarning.Text = "提示信息：" + "删除cflow数据失败，请联系系统管理员！";
                    }
                    else
                    {
                        lblTransferWarning.Text = "提示信息：" + "删除cflow数据成功！";
                        transfer_id.Value = "";
                    }
                }
                bindBankTransfer();
                gridBank.Selection.UnselectAll();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void checkAgentSale_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkAgentSale.Checked)
                {
                    tbNormal.Visible = false;
                    tbAgentSale.Visible = true;
                    agentLoad();
                }
                else
                {
                    tbNormal.Visible = true;
                    tbAgentSale.Visible = false;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        private void agentLoad()
        {
            try
            {
                DataSet dsAgent = SaleAgentDao.getSaleAgentList("N");
                dropAgentName.DataSource = dsAgent;
                dropAgentName.DataTextField = "sale_agent_name";
                dropAgentName.DataValueField = "sale_agent_id";
                dropAgentName.DataBind();

                DataSet dsProd = ProductDao.getAgentProduct(dropAgentName.SelectedItem.Value.Trim());
                dropProdAgent.DataSource = dsProd;
                dropProdAgent.DataTextField = "product_name";
                dropProdAgent.DataValueField = "product_code";
                dropProdAgent.DataBind();
                dropDueProdAgent.DataSource = dsProd;
                dropDueProdAgent.DataTextField = "product_name";
                dropDueProdAgent.DataValueField = "product_code";
                dropDueProdAgent.DataBind();
                ListItem liAll = new ListItem();
                liAll.Text = "取款";
                liAll.Value = "";
                dropDueProdAgent.Items.Insert(0, liAll);

                string[] strDiscount = dropProdAgent.SelectedItem.Text.Split('|');
                txtAgentDiscount.Value = strDiscount[2].ToString().Trim();
                txtRealSaleDiscount.Value = strDiscount[2].ToString().Trim();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                int ideal_no = int.Parse(hiddealno.Value.ToString());
                bool bok = DealDao.SubmitMMDeal(ideal_no, "IV");
                if (bok)
                {
                    Response.Write("<script language=javascript>alert('数据提交成功，等待后台人员审核！');window.location.href='eCard_input.aspx';</script>");
                }
                else
                {
                    lblSubmitWarning.Text = "提示信息：数据提交失败，请联系管理员！";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                int ideal_no = int.Parse(hiddealno.Value.ToString());
                //删除交易后的卡号可以继续用。
                bool bok = DealDao.CancelMMDeal(ideal_no);
                if (bok)
                {
                    Response.Write("<script language=javascript>alert('数据已经删除！');window.location.href='eCard_input.aspx';</script>");
                }
                else
                {
                    lblSubmitWarning.Text = "提示信息：数据删除失败，请联系管理员！";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        private void bindAttachList(int deal_no)
        {
            try
            {
                int iDeal_no = deal_no;
                DataSet dsAttachList = AttachDao.getAttachList(iDeal_no, "IA");
                gridAttach.DataSource = dsAttachList;
                gridAttach.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                if (hiddealno.Value.ToString().Trim().Equals(""))
                {
                    lblAttachWarning.Text = "请先完成销售数据的保存！";
                    Response.Write("请先完成销售数据的保存！");
                    return;
                }
                int deal_no = int.Parse(hiddealno.Value.ToString());
                string view_name = "";
                string attach_name = "";
                string attach_type = "";
                string attach_size = "";
                string attach_url = "";

                if (this.FileUpload1.PostedFile.ContentLength != 0 && this.FileUpload1.PostedFile.ContentLength < (10 * 1024 * 1024))
                {
                    FileInfo fileInfo = new FileInfo(FileUpload1.FileName);
                    attach_name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileInfo.Name;
                    string savePathName = MapPath(UploadDirectory) + attach_name;
                    FileUpload1.SaveAs(savePathName);

                    view_name = fileInfo.Name;
                    attach_type = FileUpload1.PostedFile.ContentType.ToString();
                    attach_size = FileUpload1.PostedFile.ContentLength / 1024 + "K";
                    attach_url = "/eCard/tempUpload/" + attach_name;
                    AttachDao.AddAttach(deal_no, view_name, attach_name, attach_type, attach_size, attach_url, "IA");
                    bindAttachList(deal_no);
                }
                else
                {
                    lblAttachWarning.Text = "上传的文件的大小超出范围!";
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("eCard_input.btnUpload_Click", ex.ToString());
                Response.Write(ex.ToString());
            }
        }

        protected void gridAttach_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e)
        {
            try
            {
                if (e.ButtonID != "Delete") return;
                int attach_id = int.Parse(gridAttach.GetRowValues(e.VisibleIndex, "attach_id").ToString());
                int temp_deal_no = int.Parse(gridAttach.GetRowValues(e.VisibleIndex, "deal_no").ToString());
                AttachDao.DeleteAttach(attach_id);
                bindAttachList(temp_deal_no);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
}